//package com.ling.pl.core.commons.springmvc;
//
//import com.ling.pl.core.commons.springmvc.annotation.LoginAnnotation;
//import com.ling.pl.core.commons.springmvc.constant.FailStatus;
//import com.ling.pl.core.commons.springmvc.constant.ResultField;
//import com.ling.pl.core.commons.springmvc.constant.ResultStatus;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.OutputStream;
//import java.io.OutputStreamWriter;
//import java.io.PrintWriter;
//import java.net.URLEncoder;
//
///**
// * Created by bo.wang on 2016/9/26.
// */
//public class TestHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
//    //
//    final Logger logger = LoggerFactory.getLogger(getClass());
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用（Controller方法调用之前）");
//    	if(handler instanceof HandlerMethod){
//	    	boolean pass=true;
//	    	int failStatus=0;
//	        HandlerMethod handlerMethod=(HandlerMethod) handler;
//	//        AnonymousAnnotation anonymousAnnotation = handlerMethod.getMethodAnnotation(AnonymousAnnotation.class);
//	//        if(anonymousAnnotation!=null){
//	//            return true;
//	//        }
////	        for(MethodParameter parameter:handlerMethod.getMethodParameters()){
////	        	System.out.println(parameter.getParameterName());
////	        	System.out.println(parameter.getParameterType());
////	        	if(parameter.getParameterType()==Map.class){
////	        	}
////	        }
////	        LoginAnnotation loginAnnotation = handlerMethod.getMethodAnnotation(LoginAnnotation.class);
////	        if(loginAnnotation!=null){
////	            if(ContextUtils.getCurrentUser()!=null){
////	            	pass=true;
////	            }else {
////	            	failStatus= FailStatus.NOTLOGIN;
////	            	pass=false;
////				}
////	        }
////            pass=false;
//	        //没有访问权限 异常处理
//	        ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class);
//
//	        if(!pass){
//		        if (responseBody==null) {
//		            //传统的登录页面
//		            StringBuilder sb = new StringBuilder();
//		            sb.append(request.getContextPath());
//		            sb.append("/oprst.jsp?oprst=false&opmsg=").append(URLEncoder.encode("没有权限访问","utf-8"));
//		            response.sendRedirect(sb.toString());
//		        } else{
//		            //ajax类型的登录提示
//		            response.setCharacterEncoding("utf-8");
//		            response.setContentType("text/html;charset=UTF-8");
//		            OutputStream out = response.getOutputStream();
//		            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,"utf-8"));
//		            pw.println("{\""+ ResultField.STATUS+"\":\""+ ResultStatus.FAIL+"\",\""+ResultField.ERROCODE+"\":"+failStatus+",\""+ResultField.ERROMESSAGE+"\":\""+"没有权限访问"+"\"}");
//		            pw.flush();
//		            pw.close();
//		        }
//		        return false;
//	        }
//	        return true;
//    	}else{
//    		return true;
//    	}
//    }
//
//	@Override
//	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
//						   ModelAndView modelAndView) throws Exception {
//		System.out.println(">>>MyInterceptor2>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
//	}
//
//	@Override
//	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
//			throws Exception {
//		System.out.println(">>>MyInterceptor2>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
//	}
//}
